package eerbaar.client.exp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TokenIterator implements Iterator<Token> {

    private final List<Token> tokens;

    public TokenIterator(List<Token> tokens) {
        this.tokens = new ArrayList<Token>(tokens);
    }

    public TokenIterator(Token token) {
        this.tokens = new ArrayList<Token>();
        this.tokens.add(token);
    }

    public TokenIterator(TokenIterator copy) {
        this.tokens = new ArrayList<Token>(copy.tokens);
    }

    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        return toString().equals(obj);
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public boolean hasNext() {

        return (this.tokens.size() > 0);
    }

    @Override
    public Token next() {

        return this.tokens.remove(0);
    }

    @Override
    public void remove() {
        //
    }

    @Override
    public String toString() {

        String s = "";
        for (Token token : this.tokens) {
            if (s.length() > 0) {
                s += " ";
            }
            s += token;
        }

        return s;
    }

}
